using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class TypeManager
	{
		#region CONSTRUCTORS
		static TypeManager()
		{
			_data = new TypeData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static TypeData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static bool Insert( string typeName,  string description)
		{
			_error = "";
			try
			{
				object[] values = new object[2];
				values[0] = Util.ReplaceNullTo(typeName, DBNull.Value);
				values[1] = Util.ReplaceNullTo(description, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Insert( TypeInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[2];
				values[0] = Util.ReplaceNullTo(info.TypeName, DBNull.Value);
				values[1] = Util.ReplaceNullTo(info.Description, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( int typeID,  string typeName,  string description)
		{
			_error = "";
			try
			{
				object[] values = new object[3];
				values[0] = typeID;
				values[1] = Util.ReplaceNullTo(typeName, DBNull.Value);
				values[2] = Util.ReplaceNullTo(description, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( TypeInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[3];
				values[0] = info.TypeID;
				values[1] = Util.ReplaceNullTo(info.TypeName, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.Description, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( int typeID)
		{
			_error = "";
			try
			{
				object[] values = { typeID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( TypeInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.TypeID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static TypeInfo Select( int typeID)
		{
			_error = "";
			try
			{
				object[] values = { typeID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				TypeInfo info = new TypeInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return TypeManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return TypeManager.Search( "", "", pageIndex, pageSize);
		}
		
		#endregion
		
	}
}